#include "pidparameterset.h"
#include "ui_pidparameterset.h"
Q_GLOBAL_STATIC(PidParameterSet,theInstance);
PidParameterSet::PidParameterSet(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::PidParameterSet)
{
    ui->setupUi(this);
    this->setWindowTitle("PID参数设置界面");
    this->setWindowIcon(QIcon(":res/res/PID.ico"));
    initSpinboxRange();
}

PidParameterSet *PidParameterSet::instance()
{
return theInstance;
}

PidParameterSet::~PidParameterSet()
{
    delete ui;
}

void PidParameterSet::displayPidParameterSetTable()
{
    // 2. 创建模型并绑定表
         model = new QSqlTableModel(this,UserMysqlS::instance()->getDataBase());
         model->setTable("pidparameterset");

         // 3. 设置列标题（字段名到中文的映射）
         model->setHeaderData(0,Qt::Horizontal,"id");
         model->setHeaderData(1, Qt::Horizontal, "PIDControlName");
         model->setHeaderData(2, Qt::Horizontal, "SetpointValue");
         model->setHeaderData(3, Qt::Horizontal, "OutputLimitsUpper");
         model->setHeaderData(4, Qt::Horizontal, "OutputLimitsLower");
         model->setHeaderData(5, Qt::Horizontal, "Kp");
         model->setHeaderData(6, Qt::Horizontal, "Ti");
         model->setHeaderData(7, Qt::Horizontal, "Td");
         model->setHeaderData(8, Qt::Horizontal, "Deadband");
         model->setHeaderData(9, Qt::Horizontal, "SamplingTime");


         // 4. 加载数据
         if (!model->select()) {
             QMessageBox::critical(this, "错误", "查询失败: " + model->lastError().text());
             //UserMysqlS::instance()->getDataBase()->close();
             return;
         }

         // 5. 创建视图并绑定模型
        // QTableView *tableView = new QTableView(this);
         ui->tableViewPIDParameterSet->setModel(model);
         //tableView->setModel(model);
         ui->tableViewPIDParameterSet->resizeColumnsToContents();//自适应
         ui->tableViewPIDParameterSet->setSortingEnabled(true);//自动排序

         ui->tableViewPIDParameterSet->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//可拉伸

         // 6. 布局
 //        QVBoxLayout *layout = new QVBoxLayout(this);
 //        layout->addWidget( ui->tableViewSensorDisplay);
 //        setLayout(layout);

         // 注意：此处未关闭数据库连接，因为模型可能后续需要操作


}

void PidParameterSet::on_pushButtonExit_clicked()
{
this->close();
}


void PidParameterSet::on_pushButtonConfirm_clicked()
{
//获取当前的id号
    int currentId=ui->comboBoxPIDControlName->currentIndex()+1;        int currentSetpointValue=ui->spinBoxSetpointValue->value();
    int currentOutputLimitsUpper=ui->spinBoxOutputLimitsUpper->value();
    int currentOutputLimitsLower=ui->spinBoxOutputLimitsLower->value();
    int currentKp=ui->spinBoxKp->value();
    int currentTi=ui->spinBoxTi->value();
    int currentTd=ui->spinBoxTd->value();
    int currentDeadband=ui->spinBoxDeadband->value();
    int currentSamplingTime=ui->spinBoxSamplingTime->value();
    setPidParameters(currentId, currentSetpointValue, currentOutputLimitsUpper, currentOutputLimitsLower, currentKp, currentTi, currentTd, currentDeadband, currentSamplingTime);

}







void PidParameterSet::on_pushButtonRefresh_clicked()
{
    //禁用按钮防止重复点击
            ui->pushButtonRefresh->setEnabled(false);
            QApplication::processEvents();  // 立即更新UI

            // 重新加载数据
            if (!model->select()) {
                QMessageBox::critical(this, "错误", "刷新失败: " + model->lastError().text());
            } else {
                // 滚动到最新数据（假设按时间降序排序）
                ui->tableViewPIDParameterSet->scrollToTop();
            }

            // 恢复按钮状态
            ui->pushButtonRefresh->setEnabled(true);
}

void PidParameterSet::initSpinboxRange()
{

ui->spinBoxSetpointValue->setRange(0,2000);
ui->spinBoxOutputLimitsUpper->setRange(0,100);
ui->spinBoxOutputLimitsLower->setRange(0,100);
ui->spinBoxKp->setRange(0,100);
ui->spinBoxTi->setRange(0,100);
ui->spinBoxTd->setRange(0,100);
ui->spinBoxDeadband->setRange(0,100);
ui->spinBoxSamplingTime->setRange(0,100);

}

void PidParameterSet::setPidParameters(int id, int SetpointValue, int OutputLimitsUpper, int OutputLimitsLower, int Kp, int Ti, int Td, int Deadband, int SamplingTime)
{
    //获取未改变之前的各种参数
    int lastSetpointValue=UserMysqlS::instance()->getPidParameters(id,"SetpointValue");
    int lastOutputLimitsUpper=UserMysqlS::instance()->getPidParameters(id,"OutputLimitsUpper");
    int lastOutputLimitsLower=UserMysqlS::instance()->getPidParameters(id,"OutputLimitsLower");
    int lastKp=UserMysqlS::instance()->getPidParameters(id,"Kp");
    int lastTi=UserMysqlS::instance()->getPidParameters(id,"Ti");
    int lastTd=UserMysqlS::instance()->getPidParameters(id,"Td");
    int lastDeadband=UserMysqlS::instance()->getPidParameters(id,"Deadband");
    int lastSamplingTime=UserMysqlS::instance()->getPidParameters(id,"SamplingTime");
    if(SetpointValue!=lastSetpointValue)
    {
        //设置新值
       if(UserMysqlS::instance()->setPidParameters(id,"SetpointValue",SetpointValue))
       {
           QMessageBox::information(this,"success","更新SetpointValue成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新SetpointValue失败");
       }

    }
    else
    if(OutputLimitsUpper!=lastOutputLimitsUpper)
    {
        //设置新值
       if(UserMysqlS::instance()->setPidParameters(id,"OutputLimitsUpper",OutputLimitsUpper))
       {
           QMessageBox::information(this,"success","更新OutputLimitsUpper成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新OutputLimitsUpper失败");
       }
    }
    if(OutputLimitsLower!=lastOutputLimitsLower)
    {
        //设置新值
       if(UserMysqlS::instance()->setPidParameters(id,"OutputLimitsLower",OutputLimitsLower))
       {
           QMessageBox::information(this,"success","更新OutputLimitsLower成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新OutputLimitsLower失败");
       }
    }
    if(Kp!=lastKp)
    {
        //设置新值
       if(UserMysqlS::instance()->setPidParameters(id,"Kp",Kp))
       {
           QMessageBox::information(this,"success","更新Kp成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新Kp失败");
       }
    }
    if(Ti!=lastTi)
    {
        //设置新值
       if(UserMysqlS::instance()->setPidParameters(id,"Ti",Ti))
       {
           QMessageBox::information(this,"success","更新Ti成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新Ti失败");
       }
    }
    if(Td!=lastTd)
    {
        //设置新值
       if(UserMysqlS::instance()->setPidParameters(id,"Td",Td))
       {
           QMessageBox::information(this,"success","更新Td成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新Td失败");
       }
    }
    if(Deadband!=lastDeadband)
    {
        //设置新值
       if(UserMysqlS::instance()->setPidParameters(id,"Deadband",Deadband))
       {
           QMessageBox::information(this,"success","更新Deadband成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新Deadband失败");
       }
    }
    if(SamplingTime!=lastSamplingTime)
    {
        //设置新值
       if(UserMysqlS::instance()->setPidParameters(id,"SamplingTime",SamplingTime))
       {
           QMessageBox::information(this,"success","更新SamplingTime成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新SamplingTime失败");
       }
    }

}


void PidParameterSet::on_comboBoxPIDControlName_currentIndexChanged(int index)
{
//ui->spinBoxSetpointValue->setValue(index);
}


void PidParameterSet::on_comboBoxPIDControlName_highlighted(int index)
{
    //获取当前各种设置的值
    index++;
    int currentSetpointValue=UserMysqlS::instance()->getPidParameters(index,"SetpointValue");
    int currentOutputLimitsUpper=UserMysqlS::instance()->getPidParameters(index,"OutputLimitsUpper");
    int currentOutputLimitsLower=UserMysqlS::instance()->getPidParameters(index,"OutputLimitsLower");
    int currentKp=UserMysqlS::instance()->getPidParameters(index,"Kp");
    int currentTi=UserMysqlS::instance()->getPidParameters(index,"Ti");
    int currentTd=UserMysqlS::instance()->getPidParameters(index,"Td");
    int currentDeadband=UserMysqlS::instance()->getPidParameters(index,"Deadband");
    int currentSamplingTime=UserMysqlS::instance()->getPidParameters(index,"SamplingTime");
    ui->spinBoxSetpointValue->setValue(currentSetpointValue);
    ui->spinBoxOutputLimitsUpper->setValue(currentOutputLimitsUpper);
    ui->spinBoxOutputLimitsLower->setValue(currentOutputLimitsLower);
    ui->spinBoxKp->setValue(currentKp);
    ui->spinBoxTi->setValue(currentTi);
    ui->spinBoxTd->setValue(currentTd);
    ui->spinBoxDeadband->setValue(currentDeadband);
    ui->spinBoxSamplingTime->setValue(currentSamplingTime);

}

